#include "compile_run.hpp"
#include "../comm/httplib.h"
using namespace ns_compile_and_runner;
using namespace httplib;


void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}
// 使用：./compile_server port (端口号自己指定)
int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    //提供编译服务，打包形成一个网络服务
    Server svr;
    // svr.Get("/hello",[](const Request& req,Response& resp){
    //     resp.set_content("hello,http!","text/plain;charset=utf-8");
    // });

    svr.Post("/compile_and_run",[](const Request& req,Response& resp)
    {
        //用户请求的正文是我们要的json串
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
        }
    });

    svr.listen("0.0.0.0",atoi(argv[1]));
    return 0;
}
